package com.bk.game.screen;

import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.bk.game.global.BkGlobal;

/**
 * 描述:纪录
 * 
 * @author linqiang(866116)
 * @Since 2012-9-5
 */
public class RankScreen implements Screen, GestureListener, InputProcessor {

	private TextureAtlas rank;
	private SpriteBatch batch;

	private Sprite backBtn;
	private TextureRegion bg;
	private Rectangle backRect;
	private BitmapFont rankFonts;
	private List<Integer> ranks;
	private float cy;

	@Override
	public void dispose() {
		rankFonts.dispose();
		rank.dispose();
		batch.dispose();
	}

	@Override
	public void hide() {

	}

	@Override
	public void pause() {

	}

	@Override
	public void render(float arg0) {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
		cy = 770;
		batch.begin();
		batch.draw(bg, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		for (int i = 0; i < 5; i++) {
			rankFonts.draw(batch, ranks.get(i) + "", 230 * BkGlobal.factorx, cy * BkGlobal.factory);
			cy -= 90;
		}
		backBtn.draw(batch);
		batch.end();
	}

	@Override
	public void resize(int arg0, int arg1) {

	}

	@Override
	public void resume() {

	}

	@Override
	public void show() {
		Gdx.input.setCatchBackKey(true);
		ranks = BkGlobal.getInstance().getRanks();

		rankFonts = new BitmapFont(Gdx.files.internal("font.fnt"), Gdx.files.internal("font.png"), false);
		rankFonts.setColor(Color.BLACK);
		rankFonts.setScale(0.8f * BkGlobal.factory);

		batch = new SpriteBatch(); // 实例化
		rank = BkGlobal.manager.get("rank.pack", TextureAtlas.class);
		bg = rank.findRegion("bg_rank");
		backBtn = new Sprite(rank.findRegion("btn_rank1"));
		backBtn.setSize(98 * BkGlobal.factorx, 70 * BkGlobal.factory);
		backBtn.setPosition(0, 110 * BkGlobal.factory);
		backRect = backBtn.getBoundingRectangle();
		backBtn.setColor(0.5f, 0.5f, 0.5f, 0f);
		// 将当前Screen加入按键和手势监听队列
		InputMultiplexer multiplexer = new InputMultiplexer();
		multiplexer.addProcessor(new GestureDetector(this));
		multiplexer.addProcessor(this);
		Gdx.input.setInputProcessor(multiplexer);
	}

	@Override
	public boolean fling(float arg0, float arg1) {
		backBtn.setColor(0.5f, 0.5f, 0.5f, 0f);
		return false;
	}

	@Override
	public boolean longPress(int arg0, int arg1) {
		return false;
	}

	@Override
	public boolean pan(int arg0, int arg1, int arg2, int arg3) {
		return false;
	}

	@Override
	public boolean pinch(Vector2 arg0, Vector2 arg1, Vector2 arg2, Vector2 arg3) {
		return false;
	}

	@Override
	public boolean tap(int x, int y, int count) {
		int ty = Gdx.graphics.getHeight() - y;
		if (backRect.contains(x, ty)) {
			backBtn.setColor(0.5f, 0.5f, 0.5f, 0f);
			BkGlobal.currentGame.setScreen(BkGlobal.mainScreen);
		}
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int pointer) {
		int ty = Gdx.graphics.getHeight() - y;
		if (backRect.contains(x, ty)) {
			BkGlobal.btnSound.play();
			backBtn.setColor(0.5f, 0.5f, 0.5f, 1f);
		}
		return false;
	}

	@Override
	public boolean zoom(float arg0, float arg1) {
		return false;
	}

	@Override
	public boolean keyDown(int arg0) {
		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {
		return false;
	}

	@Override
	public boolean keyUp(int arg0) {
		if (arg0 == Input.Keys.BACK) {
			BkGlobal.currentGame.setScreen(BkGlobal.mainScreen);
		}
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {
		return false;
	}

	@Override
	public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
		return false;
	}

	@Override
	public boolean touchDragged(int arg0, int arg1, int arg2) {
		return false;
	}

	@Override
	public boolean touchMoved(int arg0, int arg1) {
		return false;
	}

	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		return false;
	}
}
